// 入口文件
import Vue from 'vue'

// 导入app
import app from './App.vue'

//导入moment时间插件
import moment from 'moment'

// 导入缩略图插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)


// 定义全局过滤器
Vue.filter('dateFormat',function(datastr,pattern="YYYY-MM-DD HH:mm:ss") {
return moment(datastr).format(pattern)
})

// 导入路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import router from './router.js'



// 導入axios
import axios from 'axios'
Vue.prototype.$http = axios
axios.defaults.baseURL='http://vue.studyit.io'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

// 导入vuex
import Vuex from 'vuex'
Vue.use(Vuex)

var car=JSON.parse(localStorage.getItem('car') || '[]')
var store =new Vuex.Store({
    state:{//this.$store.state
        car:car
    },
    mutations:{//this.$store.commit
        addToCar(state,goodsinfo){
            var flag=false
            state.car.some(item=>{
                if(item.id===goodsinfo.id){
                    item.count+=parseInt(goodsinfo.count)
                    flag=true
                    return true
                }
            })
            if(!flag){
                state.car.push(goodsinfo)
            }
           localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateGoodsInfo(state,goodsinfo){
            state.car.some(item=>{
                if(item.id==goodsinfo.id){
                    item.count=parseInt(goodsinfo.count)
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        removeFormCar(state,id){
            state.car.some((item,i)=>{
                if(item.id==id){
                    state.car.splice(i,1)
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateGoodSelected(state,info){
            state.car.some(item=>{
                if(item.id==info.id){
                    item.selected=info.selected
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },
    getters:{//this.$store.getters
        getAllcount(state){
           var c=0
           state.car.forEach(item=>{
               c+=item.count
           })
           return c
        },
        getGoodsCount(state){
            var o={}
            state.car.forEach(item=>{
                0[item.id]=item.count
            })
            return o
        },
        getGoodsSelected(state){
            var o={}
            state.car.forEach(item=>{
                o[item.id]=item.selected
                return o
            })
        },
        getGoodsCountAndAmount(state){
            var o={
                count:0,
                amount:0
            }
            state.car.forEach(item=>{
                if(item.selected){
                    o.count+=item.count
                    o.amount=item.count*item.price
                }
            })
            console.log(o.amount)
            return o
        }
    }
})

// 导入mui
import './assets/lib/mui/css/mui.min.css'
// 导入图标样式
import './assets/lib/mui/css/icons-extra.css'


// 导入mint-ui和其他組件
// import { Header,Swipe,SwipeItem,Button,Lazyload} from 'mint-ui' 
// Vue.component(Header.name, Header)
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);

// mint-ui懒加载不能按需导入只能全部导入
import Mintui from 'mint-ui'
Vue.use(Mintui)
import 'mint-ui/lib/style.css'

// 设置发送地址根路径
// Vue.http.options.root = 'http://vue.studyit.io';



var vm = new Vue({
    el: '#app',
    render: c => c(app),
    router,
    store
})

